#ifndef ili9341_h
#define ili9341_h

#include "main.h"

class ili9341
{
private:
    static const uint16_t screen_width_ = 240;
    static const uint16_t screen_height_ = 320;
    static SPI_HandleTypeDef *spi_;
    static GPIO_TypeDef *dc_GPIOx_, *screen_cs_GPIOx_, *touch_cs_GPIOx_;
    static uint16_t dc_GPIO_Pin_, screen_cs_GPIO_Pin_, touch_cs_GPIO_Pin_, x, y; //数据/命令切换

    static void dc0(void) { HAL_GPIO_WritePin(dc_GPIOx_, dc_GPIO_Pin_, GPIO_PIN_RESET); }
    static void dc1(void) { HAL_GPIO_WritePin(dc_GPIOx_, dc_GPIO_Pin_, GPIO_PIN_SET); }

    static void cs1_0(void) { if(screen_cs_GPIOx_)HAL_GPIO_WritePin(screen_cs_GPIOx_, screen_cs_GPIO_Pin_, GPIO_PIN_RESET); }
    static void cs1_1(void) { if(screen_cs_GPIOx_)HAL_GPIO_WritePin(screen_cs_GPIOx_, screen_cs_GPIO_Pin_, GPIO_PIN_SET); }

    static void cs2_0(void) { if(touch_cs_GPIOx_)HAL_GPIO_WritePin(touch_cs_GPIOx_, touch_cs_GPIO_Pin_, GPIO_PIN_RESET); }
    static void cs2_1(void) { if(touch_cs_GPIOx_)HAL_GPIO_WritePin(touch_cs_GPIOx_, touch_cs_GPIO_Pin_, GPIO_PIN_SET); }

    static void send_8way(uint8_t data) { HAL_SPI_Transmit(spi_, &data, 1, 1000); }
    static uint8_t receiv_8way(void);
    static void send_8way_data_screen(uint8_t data);
    static void send_16way_data_screen(uint16_t *data, uint32_t size); //只有在发送颜色时用
    static void send_comment(uint8_t data);

    static void set_rect(uint16_t x, uint16_t y, uint16_t width, uint16_t height);
    static void screen_init(void);
    static void clear(void);

public:
    static void init(SPI_HandleTypeDef *spi, GPIO_TypeDef *dc_GPIOx, uint16_t dc_GPIO_Pin, GPIO_TypeDef *screen_cs_GPIOx, uint16_t screen_cs_GPIO_Pin, GPIO_TypeDef *touch_cs_GPIOx, uint16_t touch_cs_GPIO_Pin);
    static void display(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t *data);
    static void touch(void);
    static uint16_t get_touch_x(void) { return x; }
    static uint16_t get_touch_y(void) { return y; }
};

#endif